<?php

/*
 * Funciones de rss
 */

class Rss
{
	private $feedOut;
	
	# Constructor:
	public function Rss()
	{
		# Simplepie: Una 3rd-party que ahora un huevo de curro:
		require_once('3rd-party/simplepie/simplepie.inc');
		include_once('3rd-party/simplepie/idn/idna_convert.class.php');
	}
	
	# Pilla los posts de los feeds:
	public function getPosts($feeds)
	{
		$posts = array();
		foreach ($feeds as $nombre => $feed)
		{
			Debug::info("Bajando posts de ".$feed['url']);
			$rss = new SimplePie($feed['url']);
			$rss->handle_content_type();
			$contador = 1;
			foreach($rss->get_items() as $item)
			{
				Debug::info("  post $contador de ".MAXPOSTS.".");
				if(!is_object($item))
				{
					Debug::error("Parseando item (".$item.").");
				}
				else if(strstr($item->get_title(), "[minipost]"))
				{
					Debug::say("Minipost...pasando.");
				}
				else
				{
					$contador++;
					$timestamp = strtotime($item->get_date());
					
					# Post:
					$posts[$timestamp]['id']            = "p".$timestamp."-".$contador;
					$posts[$timestamp]['pubdate']       = $item->get_date('r');
					$posts[$timestamp]['fecha']         = $item->get_local_date("%d-%m-%y");
					$posts[$timestamp]['hora']          = $item->get_local_date("%T");
					$posts[$timestamp]['permalink']     = $item->get_permalink();
					$posts[$timestamp]['titulo']        = $item->get_title();
					$posts[$timestamp]['descripcion']   = $item->get_description();
					$posts[$timestamp]['contenido']     = $item->get_content();
					if($posts[$timestamp]['descripcion'] == $posts[$timestamp]['contenido']
						and strlen($posts[$timestamp]['contenido']) < 400)
					{
						$posts[$timestamp]['descripcion']     = Html::Recorta(Html::LimpiaBurner($item->get_description()));
						$posts[$timestamp]['contenido']       = "";
						$posts[$timestamp]['toggle']          = "";
					}
					else
					{
						$posts[$timestamp]['descripcion']   = Html::Recorta(Html::LimpiaBurner($item->get_description()));
						$posts[$timestamp]['contenido']     = Html::Limpia($item->get_content());
						$posts[$timestamp]['toggle']        = "<a onclick='ToggleContenido(\"".$posts[$timestamp]['id']
							."\"); return false;' href=\"#\">&dArr; Leer más</a>";
					}
					# Para el rss:
					$posts[$timestamp]['descripcion_rss'] = $posts[$timestamp]['descripcion'];
					$posts[$timestamp]['contenido_rss']   = $posts[$timestamp]['contenido'];
					# Autor:
					if(method_exists($item, 'get_author') and is_object($item->get_author()))
					{
						# Los del feed:
						$posts[$timestamp]['autor']        = $item->get_author()->get_name();
						$posts[$timestamp]['autor_link']   = $item->get_author()->get_link();
					}
					else
					{
						Debug::say("Sin datos de autor para feed ".$nombre);
						$posts[$timestamp]['autor'] = $nombre;
						$posts[$timestamp]['autor_link'] = $feed['url'];
					}	
					# Si han quedado vacíos pese a todo:
					if(empty($posts[$timestamp]['autor']))
					{
						$posts[$timestamp]['autor'] = $nombre;
					}
					if(empty($posts[$timestamp]['autor_link']))
					{
						$posts[$timestamp]['autor_link'] = $feed['url'];
					}
	
					$posts[$timestamp]['autor_email'] = $feed['email'];
					# Gravatar:
					$posts[$timestamp]['autor_avatar'] = "http://www.gravatar.com/avatar.php?gravatar_id="
						.md5($feed['avatar'])."&amp;size=40&amp;default=".urlencode(DEFAULT_AVATAR); 
					# Blog:
					$posts[$timestamp]['blog_title']   = $rss->get_title();
					$posts[$timestamp]['blog_url']     = $rss->get_permalink();
					$posts[$timestamp]['blog_desc']    = $rss->get_description();
					# Logo:
					$posts[$timestamp]['logo_url']     = $rss->get_image_url();
					$posts[$timestamp]['logo_link']    = $rss->get_image_link();
					$posts[$timestamp]['logo_title']   = $rss->get_image_title();
					
				}
				if($contador > MAXPOSTS) break;
			}
		}
		krsort($posts);
		#Debug::dump($posts);
		return($posts);
	}
}